<template>
  <basic-container>
    <div class="container-body">
      <el-form :inline="true" :model="query" class="demo-form-inline">
        <el-form-item label="状态:">
          <el-select size="medium" v-model="query.status" placeholder="请选择" clearable>
            <el-option label="进行中" value="1"></el-option>
            <el-option label="已结束" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="年度:">
          <el-date-picker size="medium" v-model="query.examineYear" type="year" placeholder="年度" format="yyyy" value-format="yyyy"></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button size="medium" type="primary" icon="el-icon-search" @click="onSearch" :loading="loading">搜索
          </el-button>
          <el-button size="medium" icon="el-icon-delete" @click="searchReset" :loading="loading">清空</el-button>
        </el-form-item>
      </el-form>
      <div class="btn-operate">
        <el-button v-if="permissions.examine_add" @click="showAddView" type="primary" size="medium"
                   icon="el-icon-plus">新增
        </el-button>
      </div>
      <el-table header-row-class-name="container-table-header" :data="data" size="small" style="width: 100%;margin-bottom: 20px;"
                row-key="id" border stripe v-loading="loading">
        <el-table-column prop="examineYear" label="年度">
        </el-table-column>
        <el-table-column prop="examineName" label="考评内容" min-width="100">
        </el-table-column>
        <el-table-column prop="startTime" label="开始时间">
          <template slot-scope="scope">
            <div>{{ scope.row.startTime}}</div>
          </template>
        </el-table-column>
        <el-table-column prop="endTime" label="结束日期">
          <template slot-scope="scope">
            <div>{{ scope.row.endTime}}</div>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            <div>{{ scope.row.status | examineStatus }}</div>
          </template>
        </el-table-column>
        <el-table-column
          label="操作" width="200">
          <template slot-scope="scope">
            <el-button v-if="permissions.examine_add" style="width: 50px; text-align: left;"
                       type="text" size="medium" icon="el-icon-edit" @click="showEditBox(scope.row)">编辑
            </el-button>
            <el-button v-if="permissions.examine_add" style="width: 50px; text-align: left;"
                       type="text" size="medium" icon="el-icon-upload" @click="handleGenerate(scope.row)">一键生成
            </el-button>
            <br/>
            <el-button v-if="permissions.examine_add" style="width: 50px; text-align: left;"
                       type="text" size="medium" icon="el-icon-delete" @click="handleDelete(scope.row)">删除
            </el-button>
            <el-button v-if="permissions.examine_add" style="width: 70px; text-align: left;"
                       type="text" size="medium" icon="el-icon-delete-solid" @click="handleDeleteScore(scope.row)">删除考评评分
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="container-page">
        <el-pagination background
                       @size-change="sizeChange"
                       @current-change="currentChange"
                       :current-page="page.current"
                       :page-sizes="[10, 20, 30, 40, 50, 100]"
                       :page-size="10"
                       layout="total, sizes, prev, pager, next, jumper"
                       :total="page.total">
        </el-pagination>
      </div>
    </div>

    <el-dialog :title="formTitle" :visible.sync="formBox" :close-on-click-modal="!formBox"
               :append-to-body="true" :before-close="resetForm" width="1000px" top="5vh">
      <el-form ref="optForm" :model="form" label-width="120px">
        <el-form-item label="年度" prop="examineYear" :rules="{required: true, message: '年度不能为空', trigger: 'blur'}" style="width:394px">
          <el-date-picker type="year" dataType="string" v-model="form.examineYear" placeholder="请输入年度" format="yyyy" value-format="yyyy"></el-date-picker>
        </el-form-item>
        <el-form-item label="考评" prop="examineName" :rules="{required: true, message: '考评不能为空', trigger: 'blur'}">
          <el-input v-model="form.examineName" placeholder="请输入考评度" maxlength="255" show-word-limit></el-input>
        </el-form-item>
        <el-row :gutter="10">
          <el-col :span="10">
            <el-form-item label="开始时间" prop="startTime" :rules="{required: true, message: '开始时间不能为空', trigger: ['blur', 'change']}">
              <el-date-picker v-model="form.startTime" type="datetime" placeholder="请选择发布日期" format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结束时间" prop="endTime" :rules="{required: true, message: '结束时间不能为空', trigger: ['blur', 'change']}">
              <el-date-picker v-model="form.endTime" type="datetime" placeholder="请选择发布日期" format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
                <el-button @click="resetForm" size="medium" icon="el-icon-close">取消</el-button>
               <el-button v-if="!isView" type="primary" @click="submit" size="medium" icon="el-icon-check"
                          :loading="loading">保存</el-button>
            </span>
    </el-dialog>

  </basic-container>
</template>

<script>
import {getDetail, getList, remove, submit, generate, removeScore, finishScore} from "@/api/kpi/examine";
import {statScorePersonTotal} from "@/api/kpi/scorePerson";
import {mapGetters} from "vuex";

export default {
  data() {
    return {
      importBox: false,
      isEdit: false,
      isView: false,
      viewBox: false,
      formBox: false,
      formTitle: '',
      loading: true,
      query: {},
      form: {},
      page: {
        size: 10,
        current: 1,
        total: 0
      },
      data: [],
      showUploadButton: true,
      fileList: [],
      channels: [],
      editorOption: {
        placeholder: "",
      },
    }
  },
  computed: {
    ...mapGetters(["userInfo", "permissions"])
  },
  created() {
    this.onLoad(this.page);
  },
  methods: {
    submit() {
      this.$refs['optForm'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.fileList && this.fileList.map((file) => {
            this.form.thumbUrl = file.response ? file.response.data.url:file.url
          });
          submit(this.form).then(() => {
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.resetForm();
            this.onLoad(this.page);
          }, error => {
            this.loading = false;
          });
        } else {
          return false;
        }
      });
    },
    showAddView() {
      this.formTitle = '新增';
      this.formBox = true;
    },
    showEditBox(row) {
      getDetail(row.id).then((res) => {
        this.form = res.data.data;
        this.form.examineYear = this.form.examineYear.toString();
        this.formTitle = '编辑';
        this.formBox = true;
        this.isEdit = true;
        this.fileList = [];
        this.fileList.push({url:res.data.data.thumbUrl})
      });
    },
    showViewBox(row) {
      getDetail(row.id).then((res) => {
        this.form = res.data.data;
        this.form.examineYear = this.form.examineYear.toString();
        this.workId = row.id;
        this.viewBox = true;
        this.fileList = [];
        this.fileList.push({url:res.data.data.thumbUrl})
      });
    },
    handleGenerate(row) {
      this.$confirm("确定一键生成考评评估?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.loading = true;
          return generate(row.id);
        })
        .then(() => {
          this.loading = false;
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    handleDelete(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        return remove(row.id);
      })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    handleDeleteScore(row) {
      this.$confirm("确定将选择考评评分删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        return removeScore(row.id);
      })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    resetForm() {
      this.formBox = false;
      this.isEdit = false;
      this.isView = false;
      this.form = {};
      this.formTitle = '';
      this.$refs['optForm'] && this.$refs['optForm'].resetFields();
      this.viewBox = false;
      this.fileList = [];
      this.auditBox = false;
    },
    onSearch() {
      this.page.current = 1;
      this.onLoad(this.page);
    },
    searchReset() {
      this.query = {};
      this.page.current = 1;
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.current = 1;
      this.onLoad(this.page, params);
      done();
    },
    currentChange(current) {
      this.page.current = current;
      this.onLoad(this.page, this.query);
    },
    sizeChange(size) {
      this.page.size = size;
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.current, page.size, Object.assign(params, this.query)).then(res => {
        this.loading = false;
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
      }, error => {
        this.loading = false;
      });
    },
    handleRemove(file, fileList) {
      this.fileList = fileList;
      this.showUploadButton = true
    },
    handleSuccess(response, file, fileList) {
      this.fileList = fileList;
      if (this.fileList.length >= 1) {
        this.showUploadButton = false
      }
    },
    handlePreview(file) {
      window.open(file.url ? file.url : file.response.data.url)
    },
    beforeRemove(file) {
      return this.$confirm(`确定移除 ${file.name}？`);
    },
  }
};
</script>

<style>
</style>
